package com.light.ui;

import com.light.constant.Constant;
import com.light.controller.MyController;
import javax.swing.*;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @Author MoonlightL
 * @ClassName: MyPanel
 * @ProjectName downloader
 * @Description: 面板
 * @DateTime 2023/3/10, 0010 18:04
 */
public class MyPanel extends JPanel {

    private JLabel urlLabel;

    private JLabel dirLabel;

    private JTextField urlField;

    private JTextField dirField;

    private JButton downloadButton;

    private JTextArea textArea;

    private MyController controller;

    public MyPanel(MyController controller) {
        this.controller = controller;
        this.setSize(Constant.WIN_WIDTH, Constant.WIN_HEIGHT);
        this.setLayout(null);
        this.initComponent();
    }

    private void initComponent() {

        urlLabel = new JLabel();
        urlLabel.setText("下载地址");
        urlLabel.setBounds(20, 20, 80, 32);
        this.add(urlLabel);

        urlField = new JTextField();
        // 下载腾讯qq
        urlField.setText("https://dldir1.qq.com/qqfile/qq/PCQQ9.7.3/QQ9.7.3.28946.exe");
        urlField.setBounds(85, 20, 780, 32);
        this.add(urlField);

        dirLabel = new JLabel();
        dirLabel.setText("下载目录");
        dirLabel.setBounds(20, 62, 80, 32);
        this.add(dirLabel);

        dirField = new JTextField();
        dirField.setText("D:\\");
        dirField.setBounds(85, 62, 780, 32);
        this.add(dirField);

        downloadButton = new JButton();
        downloadButton.setFont(new Font("黑体", Font.BOLD,16));
        downloadButton.setText("下载");
        downloadButton.setBounds(880, 20, 95, 32);
        downloadButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                String url = urlField.getText();
                String dir = dirField.getText();
                controller.addTask(url, dir, textArea);
            }
        });
        this.add(downloadButton);

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setBounds(20, 105, 955, 300);
        scroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                e.getAdjustable().setValue(e.getAdjustable().getMaximum());
            }
        });

        this.add(scroll);
    }
}
